using System.Web.UI;
using System.Web.UI.Adapters;

using Microsoft.SharePoint;
using Microsoft.SharePoint.Publishing.WebControls;

namespace RichImageField_ControlAdapter
{
    /// <summary>
    /// ControlAdapter for "Microsoft.SharePoint.Publishing.WebControls.RichImageField"
    /// Last edited: 20080329 by S. Schutten, Macaw
    /// 
    /// By using this ControlAdapter for the RichImageField, the rendered output of 
    /// the RichImageField-control will be corrected.
    /// 
    /// This ControlAdapter will correct the following "problems":
    /// * The "&nbsp;" at the end of the rendered output
    /// * The excess wrapping Div and Span tags of the RichImageField rendered output    
    /// * The CssClass-propetry of the RichImageField will not be set on any tag
    /// </summary>
    /// <remarks>
    /// The Assembly of the control which this ControlAdapter binds to is placed in the GAC.
    /// That assembly should also be placed in the webapplication /bin/-folder. Otherwise a 
    /// "Could not load type" exception will be thrown
    /// </remarks>
    /// <example>
    /// In the "\App_Browsers\compat.browser" file of the webapplication add:
    ///     <browsers>
    ///         <browser refID="Default">
    /// 		    <controlAdapters>
    ///     			<adapter controlType="Microsoft.SharePoint.Publishing.WebControls.RichImageField"
    ///         				 adapterType="Evides.Website.Moss2007.Website.Presentation.WebPartUI.RichImageField_Adapter, Evides.Website.Moss2007.Website.Presentation.WebPartUI , Version=1.0.0.0, Culture=neutral, PublicKeyToken=1fed9533c15552c3" />
    ///         	</controlAdapters>
    ///         </browser>
    ///     </browsers>
    /// </example>
    public class RichImageFieldControlAdapter : ControlAdapter
    {

        protected override void Render(HtmlTextWriter writer)
        {
            // Get a reference to the control to which this ControlAdapter has been connected
            RichImageField rif = this.Control as RichImageField;
            
            if (
                // Have we got a valid control instance? 
                rif != null &&
                // Have we got a valid fieldvalue?
                rif.ItemFieldValue!=null &&
                // Are we in DisplayMode?
                SPContext.Current.FormContext.FormMode == Microsoft.SharePoint.WebControls.SPControlMode.Display
               )
            { // Yes! && Yes! && Yes!
                // Retrieve the FieldValue as HTML
                // The value is stored by Sharepoint in a HTML-snippet:
                //      <a><img width=x height=x src=xxx /></a>
                string htmlvalue = rif.Field.GetFieldValueAsHtml(rif.ItemFieldValue);
                
                // Is the CssClass-property set on the RichImageField control?
                if (!string.IsNullOrEmpty(rif.CssClass))
                { // Yes!
                    // Insert the CssClass-property as an attribute on the img-tag
                    htmlvalue = htmlvalue.Replace("<img ", "<img class=\"" + rif.CssClass + "\" ");
                }

                // write out the html to the output
                writer.Write(htmlvalue);
            }
            else
            { // No
                // Render normal output
                base.Render(writer);
            }
        }

    }
}